﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Timers;

namespace Touchless.Demo
{
    class myServer
    {
        byte[] msg;
        public short targetX, targetY;
        int windowWidth, windowHeight;
        DateTime targetTime;
        Networking parent;
        Server serv;
        Thread broadcastThread;
        System.Timers.Timer timer;
        Random rand;
        int delaySeconds, delayMilliseconds;

        public myServer(Networking p, int x, int y)
        {
            parent = p; windowWidth = x; windowHeight = y;

            byte[] ipAddress = new byte[4];
            int port = 0;
            try
            {
                port = Int32.Parse(parent.portTextBox.Text);
            }
            catch (Exception e)
            {
                parent.MessageBoxShow(string.Format("Invalid port.\nError: {0}", e.Message));
                parent.toggleControls(true);
                return;
            }

            serv = new Server(port);
            delaySeconds = Int32.Parse(parent.secondsDelayUpDown.Value.ToString());
            delayMilliseconds = Int32.Parse(parent.millisecondsDelayUpDown.Value.ToString());
            timer = new System.Timers.Timer(double.Parse(delaySeconds.ToString())*1000+double.Parse(delayMilliseconds.ToString()));
            timer.Elapsed += new ElapsedEventHandler(timerElapsed);
            rand = new Random();
            msg = new byte[parent.msgSize];
            timer.Start();
            GenerateNewTarget();
        }

        private void timerElapsed(object sender, ElapsedEventArgs e)
        {
            GenerateNewTarget();
        }

        /// <summary>
        /// Generates a new random x,y coordinate and calls a method to send
        /// those along with the target time.
        /// </summary>
        private void GenerateNewTarget()
        {
            targetX = (short)rand.Next(0, windowWidth);
            targetY = (short)rand.Next(0, windowHeight);
            byte[] temp = new byte[2];
            temp = BitConverter.GetBytes(targetX);
            msg[0] = temp[0]; msg[1] = temp[1];
            temp = BitConverter.GetBytes(targetY);
            msg[2] = temp[0]; msg[3] = temp[1];
            int test = BitConverter.ToInt16(msg, 8);
            double targetSeconds = double.Parse(parent.secondsDelayUpDown.Value.ToString());
            targetTime = targetTime.AddSeconds(targetSeconds);
            double targetMilliseconds = double.Parse(parent.millisecondsDelayUpDown.Value.ToString());
            targetTime = targetTime.AddMilliseconds(targetMilliseconds);
            SendMsg();
        }

        /// <summary>
        /// Converts the position and target time at which that position should
        /// be reached to a byte array and sends these to the client.
        /// </summary>
        private void SendMsg()
        {
            byte[] temp = new byte[2];
            temp = System.BitConverter.GetBytes(short.Parse(targetTime.Hour.ToString()));
            msg[4] = temp[0]; msg[5] = temp[1];
            temp = System.BitConverter.GetBytes(short.Parse(targetTime.Minute.ToString()));
            msg[6] = temp[0]; msg[7] = temp[1];
            temp = System.BitConverter.GetBytes(short.Parse(targetTime.Second.ToString()));
            msg[8] = temp[0]; msg[9] = temp[1];
            temp = System.BitConverter.GetBytes(short.Parse(targetTime.Millisecond.ToString()));
            msg[10] = temp[0]; msg[11] = temp[1];
            serv.Broadcast(msg);
        }
    }
}
